﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FileSharing
{
    public class UploadTask
    {
        TransferConnection Connection;
        Uploads Handler;
        LocalFile Source;

        public LocalFile SourceFile { get { return Source; } }

        public UploadTask(TransferConnection conn, Uploads handler)
        {
            Connection = conn;
            Handler = handler;
        }

        public void Close()
        {
            if (Connection != null)
                Connection.Close();
        }

        public void TransferEnded()
        {
            Connection = null;
            Handler.UploadCompleted(this);
        }

        public LocalFile FindLocalFile(RemoteFile File)
        {
            if ((object)Source == null)
                Source = Handler.FindLocalFile(File);

            return Source;
        }

        public UploadItem GetUploadInfo()
        {
            UploadItem item = new UploadItem();

            if ((object)Source != null)
            {
                item.Name = Source.Name;
                item.Length = Source.Length;
            }
            else
            {
                item.Name = "";
                item.Length = 0;
            }

            item.Completed = 0;
            item.Speed = Connection.GetSpeed();

            item.Neighbor = new NeighborItem();
            item.Neighbor.Name = Connection.ConnectVizinho.Name;
            item.Neighbor.Address = Connection.ConnectVizinho.IP;
            item.Neighbor.Speed = 0;

            return item;
        }
    }
}
